package com.atguigu.flink.chapter06;

import com.atguigu.flink.bean.UserBehavior;
import com.atguigu.flink.bean.WaterSensor;
import org.apache.flink.configuration.Configuration;
import org.apache.flink.streaming.api.environment.StreamExecutionEnvironment;
import org.apache.flink.streaming.api.functions.KeyedProcessFunction;
import org.apache.flink.util.Collector;

import java.util.HashSet;

/**
 * @author liuyun
 * @title: Flink02_UV
 * @projectName flink0924
 * @description: TODO
 * @date 2022-3-7 18:53
 */
public class Flink02_UV {
    public static void main(String[] args) {
        Configuration conf = new Configuration();
        conf.setInteger("rest.port", 20000);
        StreamExecutionEnvironment env = StreamExecutionEnvironment.getExecutionEnvironment(conf);
        env.setParallelism(1);

        env
                .readTextFile("input/UserBehavior.csv")
                .map(line -> {
                    String[] data = line.split(",");
                    return new UserBehavior(
                            Long.valueOf(data[0]),
                            Long.valueOf(data[1]),
                            Integer.valueOf(data[2]),
                            data[3],
                            Long.valueOf(data[4])
                    );
                })
                .keyBy(UserBehavior::getBehavior)
                .process(new KeyedProcessFunction<String, UserBehavior, Object>() {
                    HashSet<Long> userIds = new HashSet<>();

                    @Override
                    public void processElement(UserBehavior value, KeyedProcessFunction<String, UserBehavior, Object>.Context ctx, Collector<Object> out) throws Exception {
                        if ("pv".equals(ctx.getCurrentKey())) {
                            if (userIds.add(value.getUserId())) {
                                out.collect((long) userIds.size());
                            }
                        }
                    }
                })
                .print();
        try {
            env.execute();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
